package org.pattern.pipeline;

public class StandardPipeline implements Pipeline {
    private Valve header;
    private Valve tail;

    @Override
    public Valve getHead() {
        return header;
    }

    @Override
    public Valve getTail() {
        return tail;
    }

    @Override
    public void setTail(Valve v) {
        this.tail = v;
    }

    @Override
    public void addValve(Valve v) {
        if (header == null) {
            header = v;
            v.setNext(tail);
        } else {
            Valve current = header;
            while (current != null) {
                if (current.getNext() == tail) {
                    current.setNext(v);
                    v.setNext(tail);
                    break;
                }
                current = current.getNext();
            }
        }
    }
}
